#include <xtiffio.h>

#include "GeoTiffWriter.hxx"
#include "GeoTiffImage.hxx"

GeoTiffWriter::GeoTiffWriter()
{
	_fileName[0] = '\0';
	_image = 0;
}

GeoTiffWriter::~GeoTiffWriter()
{

}

void GeoTiffWriter::write()
{
	if( _image == 0 )
	{
		fprintf(stderr, "Need a GeoTiffImage to write.\n");
		return;
	}

	TIFF		*tif = 0;
	tif = XTIFFOpen(_fileName, "w");

	if (!tif) {
		fprintf(stderr, "Can't create test TIFF file %s.\n", _fileName);
		return;
	}

	setUpTIFFDirectory(tif);

	double scale[3] = { 0.0, 0.0, 0.0 };
	double pos[6] = { 0.0, 0.0,0.0,0.0, 0.0,0.0};

	_image->getGeoScale(scale[0], scale[1], scale[2]);
	_image->getGeoModelCoord(pos[3], pos[4], pos[5]);

	TIFFSetField(tif, TIFFTAG_GEOPIXELSCALE, 3,  scale);
	TIFFSetField(tif, TIFFTAG_GEOTIEPOINTS, 6, pos);
	// TIFFSetField(tif, TIFFTAG_GEOKEYDIRECTORY, 56, tifImage->getGeoKeyDirectory());
	// TIFFSetField(tif, TIFFTAG_GEODOUBLEPARAMS, 4, tifImage->getGeoDoubleParams());
	// TIFFSetField(tif, TIFFTAG_GEOASCIIPARAMS, tifImage->getGeoAsciiParams());

	int lineSize = _image->getWidth() * _image->getChannels() * _image->getDepth() / 8;

	for (int i = 0; i < _image->getHeight(); i++)
	{
		unsigned char* buf = _image->getImageData() + i * lineSize; 

		if (TIFFWriteScanline(tif, buf, i, 0) == -1)
		{
			fprintf(stderr, "Can't write image data.\n");
			XTIFFClose(tif);
		}
	}

	XTIFFClose(tif);
}

void GeoTiffWriter::setUpTIFFDirectory(TIFF* tif)
{
	TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, _image->getWidth());
	TIFFSetField(tif, TIFFTAG_IMAGELENGTH, _image->getHeight());
	TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
	TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
	TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
	TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
	TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
	TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
	TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
	TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, _image->getWidth() * _image->getDepth() / 8 );
}

void GeoTiffWriter::write(IplImage* image, GeoTiffImage* tifImage)
{
	TIFF		*tif = 0;
	tif = XTIFFOpen(_fileName, "w");

	if (!tif) {
		fprintf(stderr, "Can't create test TIFF file %s.\n", _fileName);
		return;
	}

	setUpTIFFDirectory(tif);

	double scale[3] = { 0.0, 0.0, 0.0 };
	double pos[6] = { 0.0, 0.0,0.0,0.0, 0.0,0.0};

	tifImage->getGeoScale(scale[0], scale[1], scale[2]);
	tifImage->getGeoModelCoord(pos[3], pos[4], pos[5]);

	TIFFSetField(tif, TIFFTAG_GEOPIXELSCALE, 3,  scale);
	TIFFSetField(tif, TIFFTAG_GEOTIEPOINTS, 6, pos);
	// TIFFSetField(tif, TIFFTAG_GEOKEYDIRECTORY, 56, tifImage->getGeoKeyDirectory());
	// TIFFSetField(tif, TIFFTAG_GEODOUBLEPARAMS, 4, tifImage->getGeoDoubleParams());
	// TIFFSetField(tif, TIFFTAG_GEOASCIIPARAMS, tifImage->getGeoAsciiParams());

	int bufSize = image->width * image->nChannels * image->depth / 8;
	char* buf = (char*)malloc(bufSize);
	for (int i = 0; i < image->height; i++)
	{
		memcpy(buf, image->imageData + bufSize * i, bufSize);

		if (TIFFWriteScanline(tif, buf, i, 0) == -1)
		{
			fprintf(stderr, "Can't write image data.\n");
			XTIFFClose(tif);
		}
	}

	XTIFFClose(tif);
}